<?php
/**********************************************************
  eCode Marketing System
  Version 1.0
  Edit Campaign (/admin/campaign_edit.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require dirname(__FILE__) . "/../config.php";

// Start session if not already started
session_start();

if(!isset($_SESSION['login_t'])) {

	/* If we're not logged in, take us to the login page and
	ensure the rest of the code isn't run */

	header("Location: /admin/login.php");
	exit;
}

$pagetitle = "Edit Campaign (Admin)";
$pageheading = "<a href=\"/admin/index.php\">" . SITE_NAME . " Administration</a> &gt; Edit Campaign";
$page_content = "<hr />\n";

if(!isset($_GET['cid']) || !is_numeric($_GET['cid'])) {
	// No campaign to edit
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No campaign was selected for editing. To select a campaign for editing, use its respective \"Edit\" icon on the List Campaigns page.</div>\n";

} else if($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_GET['cid'] . "'") == false) {
	// Non-existent campaign
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The campaign selected does not exist. Please select another campaign for editing by using its respective \"Edit\" icon on the List Campaigns page.</div>\n";
} else {
	// Get campaign info
	$campaign = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_GET['cid'] . "'");

	// Convert start and end dates
	$start_date = explode("-", $campaign->start_date);
	$end_date = explode("-", $campaign->end_date);
	
	// Generate editing form
	$page_content .= "<form action=\"/admin/handler.campaign_edit.php\" method=\"post\">\n";
	$page_content .= "<table class=\"table-60\">\n";
	$page_content .= "<tr>";
	$page_content .= "<td class=\"rightalign\"><label for=\"campaign_name\">Campaign Name:</label></td>";
	$page_content .= "<td><input type=\"text\" name=\"campaign_name\" id=\"campaign_name\"";
	$page_content .= " value=\"" . $campaign->campaign_name . "\"";
	$page_content .= "></input></td>";
	$page_content .= "</tr>\n";
	$page_content .= "<tr>";
	$page_content .= "<td class=\"rightalign\"><label for=\"start_day\">Campaign Start Date:<br />(MM/DD/CCYY)</label></td>";
	$page_content .= "<td><input type=\"text\" name=\"start_month\" id=\"start_month\" maxlength=\"2\" size=\"2\"";
	$page_content .= " value=\"" . $start_date[1] . "\"";
	$page_content .= "></input> / ";
	$page_content .= "<input type=\"text\" name=\"start_day\" id=\"start_day\" maxlength=\"2\" size=\"2\"";
	$page_content .= " value=\"" . $start_date[2] . "\"";
	$page_content .= "></input> / ";
	$page_content .= "<input type=\"text\" name=\"start_year\" id=\"start_year\" maxlength=\"4\" size=\"4\"";
	$page_content .= " value=\"" . $start_date[0] . "\"";
	$page_content .= "></input>";
	$page_content .= "</td>";
	$page_content .= "<tr>";
	$page_content .= "<td class=\"rightalign\"><label for=\"end_day\">Campaign End Date:<br />(MM/DD/CCYY)</label></td>";
	$page_content .= "<td><input type=\"text\" name=\"end_month\" id=\"end_month\" maxlength=\"2\" size=\"2\"";
	$page_content .= " value=\"" . $end_date[1] . "\"";
	$page_content .= "></input> / ";
	$page_content .= "<input type=\"text\" name=\"end_day\" id=\"end_day\" maxlength=\"2\" size=\"2\"";
	$page_content .= " value=\"" . $end_date[2] . "\"";
	$page_content .= "></input> / ";
	$page_content .= "<input type=\"text\" name=\"end_year\" id=\"end_year\" maxlength=\"4\" size=\"4\"";
	$page_content .= " value=\"" . $end_date[0] . "\"";
	$page_content .= "></input>";
	$page_content .= "</td>";
	$page_content .= "</tr>\n";
	$page_content .= "<tr>";
	$page_content .= "<td colspan=\"2\" class=\"centered\">";
	$page_content .= "<input type=\"hidden\" name=\"campaign_id\" value=\"" . $campaign->campaign_id . "\"></input>";
	$page_content .= "<input type=\"submit\" value=\"Edit Campaign\"></input>";
	$page_content .= "</td>";
	$page_content .= "</tr>\n";
	$page_content .= "</table>\n";
	$page_content .= "</form>\n";

}

// Require template
require TEMPLATE_DIR . "/index.php";
?>